﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iLangl.Customer.DNN.Components;
using System.Text;
using iLangl.Customer;
using iLangl.Model;
using DotNetNuke.Services.Localization;

namespace iLangl.Customer.DNN
{
	public class ProjectViewReport
	{
		DnnModuleBase _module = null;
		public ProjectViewReport(DnnModuleBase module)
		{
			_module = module;
		}

	
		public String GetProjectReport(CustomerProject project)
		{

			StringBuilder result = new StringBuilder();
			result.Append("<div class='ReportView'>");

			result.Append("<div class='ReportGeneralInfo'>");
			WriteProperty(result, "lProject_Name", project.Name);
			WriteProperty(result, "lProject_Description", project.Description);

			if (project.WorkArea != null) 
				WriteProperty(result, "lProject_WorkArea", project.WorkArea.Name);

			if (project.Product != null) 
				WriteProperty(result, "lProject_Product", project.Product.Name);

			if (project.Owner != null) 
				WriteProperty(result, "lProject_Owner", project.Owner.Email);

			WriteProperty(result, "lProject_Status", project.Status);
			WriteProperty(result, "lProject_LockContent", project.IsLockContentDuringTranslation.ToString());
			result.Append("</div>");
			foreach (LanguagePair lp in project.GetProjectLanguagePairs())
			{
				result.Append("<div class='ReportSection'>");
				WriteLanguagePair(result, lp);
				
				foreach (Translation translation in project.GetTranslationsByLanguagePair(lp))
				{
					WriteTranslation(result, translation);
				}
				result.Append("</div>");
			}
			result.Append("</div>");

			return result.ToString();
		}
		
		

		private void WriteTranslation(StringBuilder result, Translation translation)
		{
			result.Append("<div class='ReportGeneralInfo'>");

			if(!String.IsNullOrEmpty(translation.Status))
				WriteProperty(result, "lTranslation_Status", translation.Status);

			if (!String.IsNullOrEmpty(translation.Comment))
				WriteProperty(result, "lTranslation_Comment", translation.Comment);

			if (translation.SourceContent != null)
			{
				result.AppendFormat("<p><strong>{0}</strong</p>", _module.GetLocalizedString("lSource_Content"));
				WriteContent(result, translation.SourceContent);
			}

			if (translation.TargetContent != null)
			{
				result.AppendFormat("<p><strong>{0}</strong</p>", _module.GetLocalizedString("lTarget_Content"));
				WriteContent(result, translation.TargetContent);
			}

			result.Append("</div>");
			
		}

		private void WriteContent(StringBuilder result, ContentEntity ce)
		{
			if (ce == null)
				return;
			result.Append("<div class='ReportSubSection'>");

			if (!String.IsNullOrEmpty(ce.Name))
				WriteProperty(result, "lContent_Name", ce.Name);

			if (!String.IsNullOrEmpty(ce.Type))
				WriteProperty(result, "lContent_Type", ce.Type);

			if (!String.IsNullOrEmpty(ce.Version))
				WriteProperty(result, "lContent_Version", ce.Version);
			result.Append("</div>");
		}


		private void WriteLanguagePair(StringBuilder report, LanguagePair lp)
		{
			Locale source = LocaleController.Instance.GetLocale(lp.SourceLanguage.LanguageId);
			Locale target = LocaleController.Instance.GetLocale(lp.TargetLanguage.LanguageId);

			report.AppendFormat("<p><strong>{0}</strong>-<strong>{1}</strong></p>"
				, source.EnglishName
				, target.EnglishName);
			
		}

		private void WriteProperty(StringBuilder report, string propertyName, string propertyvalue)
		{
			if (String.IsNullOrEmpty(propertyvalue))
				return;

			report.AppendFormat
				(
					"<p><strong>{0}</strong>:  {1}</p>",
					_module.GetLocalizedString(propertyName),
					propertyvalue
				);
		}
	}
}